﻿// =================================================================== 
// 项目说明
//====================================================================
// sushee。@Copy Right 2019
// 文件： SmsMessagePool.cs
// 项目名称：斑马车务
// 创建时间：2019/9/19
// 负责人：sushee
// ===================================================================
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq.Expressions;
using Dapper.Extensions;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Models;
using YouWei.Infrastructure.Repository;
namespace YouWei.Infrastructure.Service
{
	
	/// <summary>
	///短信信息池
	/// </summary>
	public class SmsMessagePoolService
	{
         protected SmsMessagePoolRepository repository;
         protected  static readonly object syncGetGruopMsgObj=new object();
         
         public  SmsMessagePoolService(){
           repository = new SmsMessagePoolRepository("CarServerConnectionString");         
         }
         
        #region 增删改查

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(SmsMessagePool entity) { return repository.Insert(entity); }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(List<SmsMessagePool> entitys) { return repository.Insert(entitys); }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Update(SmsMessagePool entity) { return repository.Update(entity); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Delete(SmsMessagePool entity) { return repository.Delete(entity); }

        /// <summary>
        /// 删除所有
        /// </summary>
        /// <returns></returns>
        public bool DeleteAll() { return repository.DeleteAll(); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public bool Delete(int primaryKey) { return repository.Delete(primaryKey); }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public SmsMessagePool SingleOrDefault(object primaryKey) { return repository.SingleOrDefault(primaryKey); }

       
        /// <summary>
        /// 查询单个实体,如果存在多条则引发异常
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public SmsMessagePool Single(object primaryKey) { return repository.Single(primaryKey); }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<SmsMessagePool> QueryAll()
        {
            return repository.QueryAll();
        }

        #endregion

        #region 单表查询帮助方法
        /// <summary>
        /// 根据谓词表达式获取一个对象，如果没有获取到对象，则返回null
        /// </summary>
        /// <param name="predicate">谓词表达式</param>
        /// <param name="useCache">是否使用缓存</param>
        /// <returns></returns>
        public SmsMessagePool QuerySingle(Expression<Func<SmsMessagePool, bool>> predicate, bool useCache = false)
        {
            return repository.QuerySingle(predicate);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="M"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="useCache"></param>
        /// <returns></returns>
        public M QuerySingle<M>(Expression<Func<M, bool>> predicate, bool useCache = false) where M : class
        {
            return repository.QuerySingle<M>(predicate, useCache);
        }
        /// <summary>
        /// 根据谓词表达式获取一个对象列表，如果没有获取到对象，则返回一个空列表
        /// </summary>
        /// <param name="predicate">谓词表达式</param>
        /// <param name="useCache">是否使用缓存</param>
        /// <returns></returns>
        public List<SmsMessagePool> QueryList(Expression<Func<SmsMessagePool, bool>> predicate, bool useCache = false)
        {
            return repository.QueryList(predicate, useCache);
        }
        /// <summary>
        /// 根据谓词表达式获取一个对象列表，如果没有获取到对象，则返回一个空列表
        /// </summary>
        /// <typeparam name="M"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="useCache"></param>
        /// <returns></returns>
        public List<M> QueryList<M>(Expression<Func<M, bool>> predicate, bool useCache = false) where M : class
        {
            return repository.QueryList<M>(predicate, useCache);
        }

        #endregion


        #region 业务逻辑

        /// <summary>
        /// 获取需要更新短信状态的一批数据
        /// </summary>
        /// <returns></returns>
        public List<SmsMessagePool> GetNeedToUpdateStateMsgs()
        {
            return repository.GetNeedToUpdateStateMsgs();
        }


        /// <summary>
        /// 获取一组需要发送短信的数据
        /// </summary>
        /// <param name="groupCount">组大小</param>
        /// <returns></returns>
	    public List<SmsMessagePool> GetGroupMsgs(int groupCount)
        {
            return repository.GetGroupMsgs(groupCount);
        }
        /// <summary>
        /// 根据品牌和车系获取一组需要发送短信的数据
        /// </summary>
        /// <param name="carBrand"></param>
        /// <param name="carSerial"></param>
        /// <param name="groupCount"></param>
        /// <returns></returns>
	    public List<SmsMessagePool> GetGroupMsgs(string carBrand, string carSerial, int groupCount)
        {
            var list = new List<SmsMessagePool>();
            //lock (syncGetGruopMsgObj)
            //{

            //}
            list = repository.GetGroupMsgs(carBrand, carSerial, groupCount);
            return list;
        }

        /// <summary>
        /// 获取一个手机号一个时间段内已经发送的短信数量
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
	    public int GetTimeRageSendCountByMobile(string mobile, int startTime, int endTime)
        {
            return repository.GetTimeRangeSendCountByMobile(mobile, startTime, endTime);
        }

        /// <summary>
        /// 获取进行中车型发送量统计
        /// </summary>
        /// <param name="startTime">开始时间yyy-MM-dd HH:mm:ss格式</param>
        /// <param name="endTime"> 结束时间yyy-MM-dd HH:mm:ss格式</param>
        /// <returns></returns>
	    public List<SmsStatisticsDto> GetStatisticsDaySend(string startTime, string endTime)
	    {
	        return repository.GetStatisticsDaySend(startTime, endTime);
	    }
        public List<SmsStatisticsDto> GetStatisticsDaySendAll(string startTime, string endTime)
        {
            return repository.GetStatisticsDaySendAll(startTime, endTime);
        }
        /// <summary>
        /// 获取进行中车型可用资源数量
        /// </summary>
        /// <returns></returns>
	    public List<SmsStatisticsDto> GetStatisticsRemain()
	    {
	        return repository.GetStatisticsRemain();
	    }

        /// <summary>
        /// 获取所有车型可用资源数量
        /// </summary>
        /// <returns></returns>
	    public List<SmsStatisticsDto> GetStatisticsAllRemain()
	    {
	        return repository.GetStatisticsAllRemain();
	    }
        #endregion

    }
	
}